# this should make a lizard walk around in some 3d matrix, collecting temperature
# the lizard walks randomly
# there is no regard for body temperature
# there is a fixed Pr(moving) = (0.5/12). this means the lizards has a 50% chance
# of moving after 2 minutes
# time steps are every 10s
# Tb is evaluated every time step



#moore.cell<-c()
#moore.cell<-c(moore.cell,round(runif(1,min=1,max=8)))


Lizard_temperature <- function (Te, TselLower=28, TselUpper=32, VTMax=39, VTMin=15, CTMax=44, CTMin=4, Pr=0.5, move=12)
# this is the function that takes a step, gets Te, works out Tb, find Pr(move) and takes another step
{                                
  #build environmental temperature array
  Tearray <-function (Te)
    {
    a<-length(Te)/100
    Temp<-array(Te, dim=c(a,10,10)
    return(Temp)
    }
  
  #I need a lookup table to turn moore-cell coordinates into cartesian coordinates
  # when standing at 9, the moore-neighbourhood looks like:
  #   1 2 3
  #   4 9 5 
  #   6 7 8
  lookup.cell<-array(c(-1,0,1,-1,1,-1,0,1,0,1,1,1,0,0,-1,-1,-1,0),dim=c(9,2))
  # find the point where the lizard starts, to give it a starting env_temp and Tb.
  start.cell<-1,array(c(round(runif(2,min=0,max=10))),dim=c(1,2))
  location<-array(c(1,start.cell[,1],start.cell[,2]),dim=c(1,3,1))
  env_temp<-c(Temp[location[1],location[2],location[3]])
  Tb<-env_temp
  #Tb starts at environmental temperature
  
  
  # this is shit because 2 deg/10s is too fast. Need the differential eqn
  calc_Tb <- function (TEnew, Tb)
    {
    a<-TEnew-Tb
    if (a< (-0.6))
      {
      newTb<-Tb-0.6
      }
      else if (a<0)
        {
        newTb<-Tb-a
        }
        else if (a<0.6)
          {
          newTb<-Tb+a
          }
          else if (a>0.6)
            {
            newTb<-Tb+0.6
            }
    return(newTb)
    }
           
             
  Prmove <- function (Tb, VTMax, VTMin, TselUpper, TselLower)
  {
  if (Tb > VTMax)
    {
    probmove<-1
    return(probmove)
    }
    if (Tb < VTMin)
      {
      probmove<-1
      return(probmove)
      }
      if (Tb > TselUpper)
        {
        #this calculates Prmove with probability from a straight line interpolation
        #starting at lowest (Tsel, pr/move) and ending at highest (VTMax,1)
        probmove <- rbinom(1,1,(((1-(pr/move))/(VTMax-TselUpper))*(Tb-VTMax)+1))
        return(probmove)
        }
          if (Tb < TselLower)
            {
            #this calculates Prmove with probability from a straight line interpolation
            #starting at lowest (Tsel, pr/move) and ending at highest (VTMin,1)
            probmove<- rbinom(1,1,((((pr/move)-1)/(TselLower-VTMin))*(Tb-VTMin)+1))
            return(probmove)
            }
          else probmove <- (rbinom(1,1,(pr/move))) 
  return(probmove)
  } 
                
        
  step <- function (probmove)     
    { 
    if (probmove==1)
      {
      #randomly pick a number in the moore-neighbourhood
      moore.step<-round(runif(1,min=1,max=8))
      }
    else
      {
      #new location = old location
      moore.step<-9
      #z=rbind(z,z[i,])
      } 
    return(moore.step)         
  }
   
   
   
   
 
  #location is the location that the lizard moves to. It will be an array of moves
  # row 1 = start.cell location
  # row 2 = srart.cell location + coordinates of new move
  # row 3 = row 2 + coordinates of new move
  # I want to loop this recalculating Tb each loop 
  
       
  while (length(location(dim(1)))<length(Temp(dim(1))))
  while (dim(length(
    {    
    for (i in 1:60)
      {
      location<-rbind(location, tail(location, n=1)+(c(0,c(lookup.cell[step(Prmove(Tb[i], VTMax, VTMin, TselUpper, TselLower)),]))))
      Te<- c(Te, Temp[tail(location, n=1)])
      Tbnew<- calc_Tb(Te[i[,Tb[i])
      Tb<-c(Tb,Tbnew)
      }
    location<-rbind(location, tail(location,n=1)+(c(1,0,0)))
    }
      
      
      
      
      
                